package demo.snippet

/**
 *
 * webServices  fo alchemyAdmin Module
 *
 * author@narnedra
 */
import demo.model._
import net.liftweb._
import common._
import scala.xml._
import net.liftweb.http._
import net.liftweb.json._
import net.liftweb.http.rest._
import net.liftweb.mapper.By
import net.liftweb.json.JsonAST.{JInt, JNothing, JNull, JValue,JArray}
import net.liftweb.json.JsonDSL._
import net.liftweb.json.JsonAST._
import JsonParser.parse
//import com.sun.codemodel.internal.JStringLiteral


/**
 *
 * Implementing rest Web Services for Methods and Compounds
 * author@narendra
 */

object FullRest extends net.liftweb.http.rest.RestHelper {




  serve {
    /**
     * WebServices to add new Method
     */
    case "webServices" :: "method" :: "add" :: Nil JsonPut JsonAST.JObject(method) -> _ => Method.add(method): JValue

    /**
     * to get list of Methods
     *
     * /webServices/methods.json
     */
    case "webServices" :: "methods" ::Nil JsonGet _ => Method.seqMethods : JValue

    /**
     * search a method by rest api
     *
     * /webServices/method/search/searchTerm.json
     */

    case "webServices" :: "method" :: "search"  :: q JsonGet _ =>
      (for {
        searchString <- q ::: S.params("q")
        method <- Method.search(searchString)
      } yield method).distinct: JValue
    /**
     * to get a list of Compounds
     *
     * /webServices/compounds.json
     */
    case "webServices" :: "compounds"  :: Nil JsonGet _ => Compound.seqCompounds : JValue

    /**
     * add new compound by method name
     *
     *
     */

    case "webServices" :: "compound" :: "add" :: Nil JsonPut JsonAST.JObject(compound) -> _ => Compound.add(compound): JValue

    /**
     *   search compounds by compound name
     *
     *   /webServices/compound/search/searchTerm.json
     */

    case "webServices" :: "compound" :: "search"  :: q JsonGet _ =>
      (for {
        searchString <- q ::: S.params("q")
        compound <- Compound.search(searchString)
      } yield compound).distinct: JValue

    /**
     * get a list of compounds by method
     *
     * /webServices/compounds/searchByMethod/searchTerm
     */

    case "webServices" :: "compounds" :: "searchByMethod"  :: q JsonGet _ =>
      (for {
        searchString <- q ::: S.params("q")
        compound <- Compound.searchByMethod(searchString)
      } yield compound).distinct: JValue
  }
}
